home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / zapem-0.000 / zapem-0 / zapem / palette.cc < prev    next >
C/C++ Source or Header  |  1995-06-03  |  2KB  |  123 lines

  1. #include "btypes.h"
  2. #include "palette.h"
  3. #include <assert.h>
  4. #include <stdlib.h>
  5.  
  6. Palette::Palette()
  7. {
  8.     for(int i=0; i<256; i++)
  9.     {
  10.         red[i]=green[i]=blue[i]=0;
  11.         allocated[i]=false;
  12.     }
  13. }
  14.  
  15. void
  16. Palette::getData(int c, int &r, int &g, int &b)
  17. {
  18.     assert(c>=0 && c<256);
  19.     r=red[c]; g=green[c]; b=blue[c];
  20. }
  21.  
  22. int
  23. Palette::request(int r, int g, int b)
  24. {
  25.     bool found=false;
  26.     int retval=0;
  27.  
  28.     assert(r>=0 && r<256 && b>=0 && b<256 && g>=0 && g<256);
  29.  
  30.     // Search for a match
  31.     for(int i=0;i<256 && !found ;i++)
  32.     {
  33.         if(allocated[i])
  34.         {
  35.             if(r==red[i] && g==green[i] && b==blue[i])
  36.             {
  37.                 found=true;
  38.                 retval=i;
  39.             }
  40.         }
  41.     }
  42.  
  43.     // If no exact match then try to allocate new color
  44.     if(!found)
  45.     {
  46.         for(int j=0;j<256 && !found ;j++)
  47.         {
  48.             if(!allocated[j])
  49.             {
  50.                 found=true;
  51.                 red[j]=r;
  52.                 green[j]=g;
  53.                 blue[j]=b;
  54.                 allocated[j]=true;
  55.                 retval=j;
  56.             }
  57.         }
  58.     }
  59.  
  60.     // If still not found then find nearest match on minimax principle
  61.     if( ! found)
  62.     {
  63.         int nearest=0, maxdist=1000, rd,gd,bd;
  64.         for(int k=0; k<256; k++)
  65.         {
  66.             rd=abs(red[k]-r);
  67.             gd=abs(green[k]-g);
  68.             bd=abs(green[k]-b);
  69.             if(rd>gd && rd>bd && rd<maxdist)
  70.                 nearest=k;
  71.             else if(bd>rd && bd>gd && bd<maxdist)
  72.                 nearest=k;
  73.             else if(gd>rd && gd>bd && gd<maxdist)
  74.                 nearest=k;
  75.         }
  76.         retval=nearest;
  77.     }
  78.     return retval;
  79. }
  80.  
  81. bool
  82. Palette::getAlloc(int i)
  83. {
  84.     return allocated[i];
  85. }
  86.  
  87. void
  88. Palette::allocate(int i, int r, int g, int b)
  89. {
  90.     red[i]=r;green[i]=g;blue[i]=b;
  91.     allocated[i]=true;
  92. }
  93.  
  94. void
  95. Palette::scan(byte *data, int len)
  96. {
  97.     for(int i=0;i<len ;i++)
  98.     {
  99.         allocated[*data]=true;
  100.         data++;
  101.     }
  102. }
  103.  
  104. void
  105. Palette::remap(byte *data, int len, Palette &inpal)
  106. {
  107.     int r,g,b;
  108.     int map[256];
  109.     for(int i=0;i<256;i++)
  110.     {
  111.         if(inpal.allocated[i])
  112.         {
  113.             inpal.getData(i,r,g,b);
  114.             map[i]=request(r,g,b);
  115.         }
  116.     }
  117.     for(int j=0;j<len;j++)
  118.     {
  119.         *data=map[*data];
  120.         data++;
  121.     }
  122. }
  123.